clear
set more off

********************************
*Bring in additional variables * 
********************************

tempfile courtvars laggedoutcomes
use "${build}/Data_ready_with_instruments.dta", clear
keep if iv_sample == 1
replace amount = 0 if amount == .
replace amount = 10000 if amount > 10000 & amount != .

label var amount "Judgment amount"
label var sheriff_eviction_completed "Sheriff completed eviction order"
label var future_case_at_same_addr "Future eviction case at same address"
merge 1:1 unique_id using  "${build}/comp_weights_for_data_ready_with_instruments.dta"
drop _merge

*Create the missing indicators
foreach var of varlist median_rent pct_below_povline pred_bla pred_his fem1 {
	replace `var'=-1 if `var'==.
	gen `var'_miss=`var'==-1
}

gen c_evict = evicted
encode districtxyear, gen(districtXyear)
encode judgeyear, gen(judgeXyear)


**************************
*Regression specification*
**************************
local cluster_ols="judgeXyear "
local cluster_iv="judgeXyear "
local stem = ""

local instrument="stringency"
local fe="districtXyear"

macro define controls="pct_below_povline median_rent joint ad_damnum fem1 pred_bla pred_his  first_case_for_name_and_address no_attorney fem1_miss pct_below_povline_miss median_rent_miss pred_bla_miss pred_his_miss" 

local outcomes_sr=" sheriff_eviction_completed_1yr sheriff_eviction_completed_2yr future_case_same_addr_1yr future_case_same_addr_2yr future_evic_same_addr_1yr future_evic_same_addr_2yr "

*Save output to variables 
gen outcome=""
gen spec=""
gen beta_est=.	
gen beta_se=.	
gen baseline_mean=.	
gen sd_of_mean=.	
gen n_obs=.
gen vnum=.

gen group = ""
gen uno = 1

* adding timing to sheriff's eviciton completed:
gen elapsed_days = orderentrdate - filing_date 
replace elapsed_days = elapsed_days + order_to_eviction_time

sum sheriff_eviction_completed if year >=2011 & year <=2015 & iv_sample == 1, de
sum sheriff_eviction_completed if year >=2011 & year <=2015 & iv_sample == 1 & (elapsed_days == . | elapsed_days<(365*2)), de

gen sheriff_eviction_completed_1yr = sheriff_eviction_completed
replace sheriff_eviction_completed_1yr = 0 if sheriff_eviction_completed==1 & (elapsed_days > 365) 

gen sheriff_eviction_completed_2yr = sheriff_eviction_completed
replace sheriff_eviction_completed_2yr = 0 if sheriff_eviction_completed==1 & (elapsed_days > (365*2)) 

label var sheriff_eviction_completed_1yr "Sheriff completed eviction order (1 yr)"
label var sheriff_eviction_completed_2yr "Sheriff completed eviction order (2 yr)"

***********************
* MAIN IV REGRESSIONS *
***********************

* EVERYONE 
eststo clear
local spec=0
foreach Group in "all" "black" "female"   {
	di "`Group'"
qui foreach var of varlist `outcomes_sr'  {	
	di "`var'"
local ifcond ""
if "`var'" == "sheriff_eviction_completed_1yr" local ifcond " if year >=2011 & year <=2015 & iv_sample == 1"
if "`var'" == "sheriff_eviction_completed_2yr" local ifcond " if year >=2011 & year <=2015 & iv_sample == 1"
if "`var'" == "future_evic_same_addr_1yr" local ifcond " if  year <=2015 & iv_sample == 1"
if "`var'" == "future_evic_same_addr_2yr" local ifcond " if year <=2014 & iv_sample == 1"

label var future_evic_same_addr_1yr "Future eviction at same addr (1 year)"
label var future_evic_same_addr_2yr "Future eviction at same addr (2 year)"

if "`Group'" == "all" local subset " "
if "`Group'" == "female" local subset " & female == 1"
if "`Group'" == "black"  local subset " & pred_bla >0.5"

local spec=`spec'+1	
	reg  `var' c_evict $controls $lagged_outcomes i.`fe' `ifcond' `subset', r cluster(`cluster_ols')
	*Populate results
	replace beta_est=_b[c_evict]  in `spec'
	replace beta_se=_se[c_evict]  in `spec'
	replace n_obs=e(N) in `spec'
	sum   `var'   if e(sample) & c_evict==0
	replace baseline_mean=r(mean) in `spec'
	replace sd_of_mean=r(sd) in `spec'
	replace outcome=`"`:variable label `var''"' in `spec'		
	replace spec="OLS more controls" in `spec'
		replace group = "`Group'" in `spec'

local spec=`spec'+1		
	ivregress 2sls `var' $controls  i.`fe'  (c_evict=`instrument') `ifcond' `subset', r cluster(`cluster_iv') 
	*Populate results
	replace beta_est=_b[c_evict]  in `spec'
	replace beta_se=_se[c_evict]  in `spec'
	replace n_obs=e(N) in `spec'
	sum   `var'   if e(sample) & c_evict==0
	replace baseline_mean=r(mean) in `spec'
	replace sd_of_mean=r(sd) in `spec'
	replace outcome=`"`:variable label `var''"' in `spec'		
	replace spec="IV more controls" in `spec'
		replace group = "`Group'" in `spec'
}
}

format beta_est %9.3fc
format beta_se %9.3fc
format n_obs %12.2gc
format sd_of_mean %9.3fc
format baseline_mean %9.3fc

keep outcome spec group beta_est beta_se baseline_mean sd_of_mean n_obs 

keep in 1/`spec'

save "${Inputs}/Cook_intermediate_outcomes_ivols.dta", replace 

